#博客编辑页
import time
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from common.Utils import BlogDriver

class BlogEdit:
    url=""
    driver=""
    def __init__(self):
        self.url="http://8.137.19.140:9090/blog_edit.html"
        self.driver=BlogDriver.driver
        self.driver.get(self.url)

    def EditTestByBlogPublish(self):
        """
        测试发布博客
        :return:
        """
        print("----测试发布博客----")
        #输入标题
        self.driver.find_element(By.CSS_SELECTOR,"#title").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#title").send_keys("自动化测试-发布博客")
        #编辑区域默认不为空，暂不处理
        #点击发布按钮
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        #假设刚才发布的文章是第1篇
        #添加显示等待
        WebDriverWait(self.driver, 5).until(EC.title_is("博客列表页"))

        #检查刚才的博客是否发布成功
        title=self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div:nth-child(1) > div.title").text
        assert title=="自动化测试-发布博客"
        print("发布成功")

        #添加显示等待,等待查看全文按钮加载完成
        WebDriverWait(self.driver, 5).until(EC.presence_of_element_located((By.CSS_SELECTOR,"body > div.container > div.right > div:nth-child(1) > a")))

        #添加屏幕截图
        BlogDriver.SaveScreenCut(name=type(self).__name__)

        print("----测试结束----\n")

    def EditTestByBlogRepost(self):
        """
        对已有博客重新发布
        :return:
        """
        print("----测试更新博客----")

        #打开博客列表页的网址
        self.driver.get("http://8.137.19.140:9090/blog_list.html")

        #添加显示等待,点击查看全文按钮
        WebDriverWait(self.driver, 5).until(EC.presence_of_element_located((By.CSS_SELECTOR,"body > div.container > div.right > div > a")))
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div > a").click()

        # 添加显示等待
        WebDriverWait(self.driver, 5).until(EC.presence_of_element_located(
            (By.CSS_SELECTOR, "body > div.container > div.right > div > div.operating > button:nth-child(1)")))
        self.driver.find_element(By.CSS_SELECTOR,
                                 "body > div.container > div.right > div > div.operating > button:nth-child(1)").click()

        #输入标题
        self.driver.find_element(By.CSS_SELECTOR,"#title").send_keys("2")

        #点击发布按钮
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()

        #添加显示等待
        WebDriverWait(self.driver, 5).until(EC.title_is("博客列表页"))

        #检查刚才的博客是否发布成功
        title=self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div:nth-child(1) > div.title").text
        assert title=="自动化测试-发布博客2"
        print("发布成功")

        #添加显示等待,等待查看全文按钮加载完成
        WebDriverWait(self.driver, 5).until(EC.presence_of_element_located((By.CSS_SELECTOR,"body > div.container > div.right > div:nth-child(1) > a")))

        #添加屏幕截图
        BlogDriver.SaveScreenCut(name=type(self).__name__)

        print("----测试结束----\n")

    def EditTestByBlogDelete(self):
        """
        测试删除博客
        :return:
        """
        print("----测试删除博客----")
        #打开博客列表页的网址
        self.driver.get("http://8.137.19.140:9090/blog_list.html")

        #添加显示等待,点击查看全文按钮
        WebDriverWait(self.driver, 5).until(EC.presence_of_element_located((By.CSS_SELECTOR,"body > div.container > div.right > div > a")))
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div > a").click()

        #添加显示等待
        WebDriverWait(self.driver, 5).until(EC.presence_of_element_located((By.CSS_SELECTOR,"body > div.container > div.right > div > div.operating > button:nth-child(2)")))
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div > div.operating > button:nth-child(2)").click()

        # 等待提示框 WebDriverWait等待5秒直到弹框出现，不然会报错
        WebDriverWait(self.driver, 5).until(EC.alert_is_present())
        alert = self.driver.switch_to.alert  # 获取弹窗警告
        print("提示：" + alert.text)
        # 确认弹窗
        alert.accept()
        print("删除成功")

        #删除后自动返回博客列表页
        #添加显示等待，等待用户信息出现
        WebDriverWait(self.driver, 5).until(EC.presence_of_element_located((By.CSS_SELECTOR,"body > div.container > div.left > div > h3")))

        #添加屏幕截图
        BlogDriver.SaveScreenCut(name=type(self).__name__)

        print("----测试结束----\n")

    def EditTestByNotlogged(self):
        """
        "测试未登录状态下的博客编辑页"
        :return:
        """
        print("----测试未登录状态下进入博客编辑页----")

        # 未登录下是点击发布按钮跳转至登录页面
        # 先注销后打开网址
        self.driver.find_element(By.CSS_SELECTOR, "body > div.nav > a:nth-child(6)").click()
        self.driver.get("http://8.137.19.140:9090/blog_edit.html")

        # 点击发布按钮
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()

        # 添加显示等待
        WebDriverWait(self.driver, 20).until(EC.title_is("博客登陆页"))

        title = self.driver.title
        print("当前返回页面：" + title)

        # 添加屏幕截图
        BlogDriver.SaveScreenCut(name=type(self).__name__)
        print("----测试结束----\n")